<template>
  <basic-container>
    <h3>无锡XXX教育科技有限公司</h3>
    <avue-crud
      :option="option"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <!--<template slot="sex" slot-scope="{row}">-->
      <!--<span>{{row.sexName}}</span>-->
      <!--</template>-->
      <!--<template slot="menuRight">-->
      <!--<el-button type="primary" size="small" icon="el-icon-plus" @click="handleImport">续费VIP</el-button>-->
      <!--</template>-->
      <template slot-scope="scope" slot="menu">
        <!--<el-button icon="el-icon-check" size="small"></el-button>-->
        <!--<el-button type="text">编辑</el-button>-->
        <el-button type="text" @click="handleImport(scope.row)">续费VIP</el-button>
      </template>
    </avue-crud>
    <el-dialog title="续费VIP" :visible.sync="dialogVisible" width="45%">
      <el-form ref="form" label-width="80px">
        <el-form-item label="活动金额">
          <el-input v-model="price"></el-input>
        </el-form-item>
        <el-form-item label="延长时长">
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缴费日期">
          <el-date-picker
            v-model="form.date"
            type="datetimerange"
            format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00']"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.comment" :autosize="{ minRows: 2, maxRows: 4}" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisibleBtn">确 定</el-button>
      </span>
    </el-dialog>
  </basic-container>
</template>
<script>
import {
  Vip,
  getVipList,
  remove,
  update,
  add,
  getNotice
} from "@/api/dept/user";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  data() {
    return {
      myId: "",
      price: "",
      value: "",
      options: [
        {
          value: "1",
          label: "1个月"
        },
        {
          value: "2",
          label: "2个月"
        },
        {
          value: "3",
          label: "3个月"
        },
        {
          value: "4",
          label: "4个月"
        },
        {
          value: "5",
          label: "5个月"
        },
        {
          value: "6",
          label: "6个月"
        },
        {
          value: "7",
          label: "7个月"
        },
        {
          value: "8",
          label: "8个月"
        },
        {
          value: "9",
          label: "9个月"
        },
        {
          value: "10",
          label: "10个月"
        },
        {
          value: "11",
          label: "11个月"
        },
        {
          value: "12",
          label: "12个月"
        }
      ],
      form: {
        date: "",
        months: "",
        comment: ""
      },
      dialogVisible: false,
      tenantId: "",
      form: {},
      formInline: {
        user: "",
        region: ""
      },
      headers: {
        Authorization: `Basic c3dvcmQ6c3dvcmRfc2VjcmV0`,
        "Blade-Auth": "bearer " + getToken()
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        tip: false,
        border: true,
        index: false,
        viewBtn: false,
        addBtn: false,
        selection: false,
        columnBtn: false,
        refreshBtn: false,
        searchBtn: false,
        delBtn: false,
        editBtn: false,
        column: [
          // {
          //   label: "续费类目",
          //   prop: "name",
          //   row: true,
          //   search: false,
          //   // rules: [
          //   //   {
          //   //     required: true,
          //   //     message: "请输入姓名搜索",
          //   //     trigger: "blur"
          //   //   }
          //   // ]
          // },
          {
            label: "续费金额",
            prop: "price",
            row: true
          },
          {
            label: "延长时长",
            prop: "months",
            row: true
          },
          {
            label: "开始时间",
            prop: "startTime",
            row: true
          },
          {
            label: "结束时间",
            prop: "endTime",
            row: true
          },
          {
            label: "备注",
            prop: "comment",
            row: true
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.notice_add, false),
        viewBtn: this.vaildData(this.permission.notice_view, false),
        delBtn: this.vaildData(this.permission.notice_delete, false),
        editBtn: this.vaildData(this.permission.notice_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  created() {
    // console.log(this.$route.query.id);

    this.tenantId = this.$route.query.id;
    if (!this.tenantId) {
      this.$message({
        type: "warning",
        message: "请到公司信息开通VIp"
      });
      this.$router.go(-1);
    }
  },
  methods: {
    dialogVisibleBtn() {
      console.log(this.form.date[0]);
      var d = new Date(this.form.date[0]);
      // var datetime=d.getFullYear() + '-' + (d.getMonth() + 1 < 10 ? 0 + (d.getMonth() + 1 : d.getMonth() + 1) + '-' + d.getDate() +' ' + '00:00:00';
      var s = new Date(this.form.date[1]);
      console.log(formatDate(d));
      // var endtime=s.getFullYear() + '-' + (s.getMonth() + 1) + '-' + s.getDate() + ' ' + '00:00:00';
      const param = {
        comment: this.form.comment + "",
        months: this.value + "",
        price: Number(this.price),
        startTime: formatDate(d) + "",
        endTime: formatDate(s) + "",
        tenantId: this.myId + ""
        // tenantId:
      };

      console.log(JSON.stringify);
      Vip(param).then(res => {
        this.dialogVisible = false;
        // const data = res.data.data;
        // this.page.total = data.total;
        // this.data = data.records;
      });
      function formatTen(num) {
        return num > 9 ? num + "" : "0" + num;
      }
      function formatDate(date) {
        var date = new Date(date);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        return (
          year +
          "-" +
          formatTen(month) +
          "-" +
          formatTen(day) +
          " " +
          formatTen(hour) +
          ":" +
          formatTen(minute) +
          ":" +
          formatTen(second)
        );
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res);
      if (res.code != 200) {
        this.$message({
          message: res.msg,
          type: "error"
        });
      } else {
        this.$message({
          message: res.msg,
          type: "success"
        });
        this.onLoad(this.page);
      }
    },
    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const extension = testmsg === "xls";
      const extension2 = testmsg === "xlsx";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        this.$message({
          message: "上传文件只能是 xls、xlsx格式!",
          type: "error"
        });
      }
      if (!isLt2M) {
        this.$message({
          message: "上传文件大小不能超过 10MB!",
          type: "error"
        });
      }
      return extension || (extension2 && isLt2M);
    },
    rowSave(row, loading, done) {
      add(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowUpdate(row, index, loading, done) {
      update(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.onLoad(this.page);
    },
    searchChange(params) {
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleImport(item) {
      console.log(item);
      this.myId = item.tenantId;
      this.dialogVisible = true;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      console.log(currentPage);
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      const param = {
        tenantid: this.tenantId,
        size: page.pageSize,
        current: page.currentPage
      };
      console.log(typeof param.tenantId);
      getVipList(param).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      });
    }
  }
};
</script>
<style>
</style>
